package com.lab.es.demo.common;

import java.io.Serializable;
import java.util.List;

public class ResFactory implements Serializable {

	private static final long serialVersionUID = 1L;

	private ResFactory() {
	}

	public static ResFactory getInstance() {
		return Inner.instance;
	}

	private static class Inner {
		private static final ResFactory instance = new ResFactory();
	}

	public Res getEmptyRes() {
		Res res = new Res() {
			private static final long serialVersionUID = 1L;
		};
		return res;
	}

	public ResPaging getResPaging(List<?> records, int pageNo, int pageSize, int total) {
		ResPaging resPaging = new ResPaging();
		resPaging.setRecords(records);

		resPaging.setPageNo(pageNo);
		resPaging.setPageSize(pageSize);
		resPaging.setTotal(total);

		int pageCount = total/pageSize;
		resPaging.setPageCount(pageCount);

		if (pageNo > 1) {
			resPaging.setPrevPage(String.valueOf(pageNo - 1));
		}

		if (pageNo < pageCount) {
			resPaging.setNextPage(String.valueOf(pageNo + 1));
		}
		return resPaging;
	}

	public Res getRes(String msg, int code) {
		Res res = getEmptyRes();
		res.setCode(code);
		res.setMessage(msg);
		return res;
	}

	public Res getResRecords(Object obj, String msg, int code) {
		Res res = getEmptyRes();
		res.setCode(code);
		res.setMessage(msg);
		res.setData(obj == null ? "" : obj);
		return res;
	}
}
